package src.reflect;

import src.reflect.annotations.AutoRunClass;

import java.io.File;
import java.net.URISyntaxException;

public class Test4 {
        public static void main(String[] args) throws Exception {
                //1
                File dir = new File(Test4.class.getResource(".").toURI());
                String packageName = Test4.class.getPackage().getName();//根据Test4这个类获取它的包名
                //2
                File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
                for(File sub : subs){
                        String fileName = sub.getName();
                        String className = fileName.substring(0,fileName.indexOf("."));
                        //3
                        Class cls = Class.forName(packageName+"."+className);
                        //根据类对象判断该类是否被注解AutoRunClass标注
                        boolean tf=cls.isAnnotationPresent(AutoRunClass.class);
                        if (tf){
                                System.out.println(className+":被标注了");
                        }else {
                                System.out.println(className+":没有被标注");
                        }
                        //System.out.println(className + (tf?":被标注了":":没有被标注"));
        }
}
}

